package com.nondo.dean.dealer.controller.api.admin;

import com.nondo.dean.common.annotation.ApiVersion;
import com.nondo.dean.common.tool.ValidateUtil;
import com.nondo.dean.common.web.View;
import com.nondo.dean.dealer.domain.ActivityTemplate;
import com.nondo.dean.dealer.service.ActivityTemplateService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.BindingResult;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

/**
 * @author xiaofan
 * @Description: 模板管理
 * @throws
 * @date 2018-05-1411:23
 * @Version
 */
@RestController("apiAdminDealerActivityTemplate")
@RequestMapping("/{v}/admin/activity/template")
public class ActivityTemplateController {

    @Autowired
    ActivityTemplateService activityTemplateService;

    /**
     * 添加模板
     * @param request
     * @param response
     * @param activityTemplate 产品类型信息 {@link ActivityTemplate}
     * @return
     */
    @ApiVersion(1)
    @RequestMapping("/add")
    public View<ActivityTemplate> add(HttpServletRequest request, HttpServletResponse response,
                                      @Validated @RequestBody ActivityTemplate activityTemplate, BindingResult result) {
        View<ActivityTemplate> view = new View<ActivityTemplate>(20000, "添加成功");

        if (result.hasErrors()) {
            view.setCode(40000);
            view.setMessage(ValidateUtil.first(result.getAllErrors()));
        } else {
            ActivityTemplate activityDb = this.activityTemplateService.selectName(activityTemplate.getName());
            if (activityDb != null) {
                return View.of500Error("模板已存在");
            }
            this.activityTemplateService.add(activityTemplate);
        }
        return view;
    }
}
